package uvt.gui.mainframe.searchroute;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import uvt.gui.mainframe.object.command.Command;
import uvt.map.JMapViewer;
import uvt.map.PositionsSettings;

public class SearchRoutePanel extends JPanel implements ActionListener {
	
	private JMapViewer mapController;
	private JPanel panelN1;
	private JPanel panelN2;
	private SearchRoute btnSearchRoute;
	private ResetRoute btnresetRoute;
	private JTextField txtMyPosLat;
	private JTextField txtMyPosLon;
	private JTextField txtArrivePosLat;
	private JTextField txtArrivePosLon;
	private JRadioButton rdbMyPos;
	private JRadioButton rdbArrivePos;
	private ButtonGroup rdbGroupPos;
	
	public SearchRoutePanel(JMapViewer m) {
		mapController = m;
	    setLayout(new GridLayout(2,1)); 
		
		rdbGroupPos = new ButtonGroup();
		
		panelN1 = new JPanel(new FlowLayout());
		panelN1.setBackground(Color.white);
		rdbMyPos = new JRadioButton("Set my position :");
		rdbMyPos.setBackground(Color.white);
		rdbMyPos.setSelected(true);
		rdbGroupPos.add(rdbMyPos);
		panelN1.add(rdbMyPos);
		Label lblMyPosLat = new Label("Latitude:");
		panelN1.add(lblMyPosLat);
		txtMyPosLat = new JTextField();
		txtMyPosLat.setEditable(false);
		txtMyPosLat.setColumns(15);
		panelN1.add(txtMyPosLat);
		Label lblMyPosLon = new Label("Longitude:");
		panelN1.add(lblMyPosLon);
		txtMyPosLon = new JTextField();
		txtMyPosLon.setEditable(false);
		txtMyPosLon.setColumns(15);
		panelN1.add(txtMyPosLon);
		btnSearchRoute = new SearchRoute(mapController,this);
		btnSearchRoute.addActionListener(this);
		panelN1.add(btnSearchRoute);
		
		add(panelN1);
		
		panelN2 = new JPanel(new FlowLayout());
		panelN2.setBackground(Color.white);
		rdbArrivePos = new JRadioButton("Set arrive position :");
		rdbArrivePos.setBackground(Color.white);
		rdbGroupPos.add(rdbArrivePos);
		panelN2.add(rdbArrivePos);
		Label lblArrivePosLat = new Label("Latitude:");
		panelN2.add(lblArrivePosLat);
		txtArrivePosLat = new JTextField();
		txtArrivePosLat.setEditable(false);
		txtArrivePosLat.setColumns(15);
		panelN2.add(txtArrivePosLat);
		Label lblArrivePosLon = new Label("Longitude:");
		panelN2.add(lblArrivePosLon);
		txtArrivePosLon = new JTextField();
		txtArrivePosLon.setEditable(false);
		txtArrivePosLon.setColumns(15);
		panelN2.add(txtArrivePosLon);
		btnresetRoute = new ResetRoute("Reset Route");
		btnresetRoute.addActionListener(this);
		panelN2.add(btnresetRoute);
		
		add(panelN2);
		
		PositionsSettings posSettings = PositionsSettings.getInstance();
		posSettings.setTxtMyPosLat(txtMyPosLat);
		posSettings.setTxtMyPosLon(txtMyPosLon);
		posSettings.setTxtArrivePosLat(txtArrivePosLat);
		posSettings.setTxtArrivePosLon(txtArrivePosLon);
		posSettings.setRdbMyPos(rdbMyPos);
		posSettings.setRdbArrivePos(rdbArrivePos);
	}
	
	public void actionPerformed(ActionEvent ac) {
		((Command)ac.getSource()).execute();
	}
	
	public void cleanPanel() {
		mapController.removeAllMapMarkers();
		txtMyPosLat.setText("");
		txtMyPosLon.setText("");
		txtArrivePosLat.setText("");
		txtArrivePosLon.setText("");
	}
	
	public void cleanMap() {
		mapController.removeAllMapMarkers();
	}

}
